package cn.com.anysdk.redis.demo.controller;

import cn.com.anysdk.redis.api.IRedisService;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Redis 测试控制器
 */
@Slf4j
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private IRedisService redisService;

    @PostMapping("/string")
    public Map<String, Object> testString(@RequestBody StringRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 设置字符串
            redisService.set(request.getKey(), request.getValue(), request.getTimeout(), TimeUnit.SECONDS);
            result.put("success", true);
            result.put("message", "设置成功");
        } catch (Exception e) {
            log.error("设置字符串失败", e);
            result.put("success", false);
            result.put("message", "设置失败：" + e.getMessage());
        }
        return result;
    }

    @GetMapping("/string/{key}")
    public Map<String, Object> getString(@PathVariable String key) {
        Map<String, Object> result = new HashMap<>();
        try {
            String value = redisService.get(key);
            result.put("success", true);
            result.put("value", value);
        } catch (Exception e) {
            log.error("获取字符串失败", e);
            result.put("success", false);
            result.put("message", "获取失败：" + e.getMessage());
        }
        return result;
    }

    @PostMapping("/hash")
    public Map<String, Object> testHash(@RequestBody HashRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 设置哈希
            redisService.hset(request.getKey(), request.getField(), request.getValue());
            result.put("success", true);
            result.put("message", "设置成功");
        } catch (Exception e) {
            log.error("设置哈希失败", e);
            result.put("success", false);
            result.put("message", "设置失败：" + e.getMessage());
        }
        return result;
    }

    @GetMapping("/hash/{key}/{field}")
    public Map<String, Object> getHash(@PathVariable String key, @PathVariable String field) {
        Map<String, Object> result = new HashMap<>();
        try {
            String value = redisService.hget(key, field);
            result.put("success", true);
            result.put("value", value);
        } catch (Exception e) {
            log.error("获取哈希失败", e);
            result.put("success", false);
            result.put("message", "获取失败：" + e.getMessage());
        }
        return result;
    }

    @Data
    public static class StringRequest {
        private String key;
        private String value;
        private long timeout = 3600; // 默认1小时
    }

    @Data
    public static class HashRequest {
        private String key;
        private String field;
        private String value;
    }
} 